package business.shareplayer;

import BaseCommon.CommLog;
import business.global.room.base.AbsBaseRoom;
import business.global.room.base.AbsRoomPos;
import business.player.Player;
import business.player.PlayerRoomInfo;
import business.rocketmq.bo.MqPlayerBo;
import business.rocketmq.bo.MqPlayerRemoveBo;
import business.rocketmq.constant.MqTopic;
import com.ddm.server.common.CommLogD;
import com.ddm.server.common.Config;
import com.ddm.server.common.redis.RedisMap;
import com.ddm.server.common.redis.RedisSet;
import com.ddm.server.common.rocketmq.MqProducerMgr;
import com.ddm.server.common.utils.BeanUtils;
import com.ddm.server.common.utils.CollectionUtils;
import com.ddm.server.common.utils.CommMath;
import com.ddm.server.common.utils.PropertiesUtil;
import com.google.gson.Gson;
import core.db.entity.clarkGame.PlayerBO;
import core.ioc.ContainerMgr;
import jsproto.c2s.cclass.LocationInfo;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yms
 * @date 2023/9/16 14:32
 * @description 中途退出玩家积分管理类
 */
public class ShareLeavePlayerMgr {
    // 类级的内部类，也就是静态的成员式内部类，该内部类的实例与外部类的实例 没有绑定关系，而且只有被调用到才会装载，从而实现了延迟加载
    private static class SingletonHolder {
        // 静态初始化器，由JVM来保证线程安全
        private static ShareLeavePlayerMgr instance = new ShareLeavePlayerMgr();
    }
    //中途离开玩家缓存key
    private final String SHARE_LEAVE_PLAYER_KEY = "shareLeavePlayerKey";

    private ShareLeavePlayerMgr() {
    }

    public static ShareLeavePlayerMgr getInstance() {
        return ShareLeavePlayerMgr.SingletonHolder.instance;
    }

    /**
     *
     * @param roomPos
     * @return
     */
    public void addLeaveSharePlayer(AbsRoomPos roomPos) {
        try {
            String key = SHARE_LEAVE_PLAYER_KEY + ":" + roomPos.getClubMemberBO().getClubID() + ":" + roomPos.getPid() + ":" + roomPos.getRoom().getRoomID();
            String value = ContainerMgr.get().getRedis().get(key);
            if(StringUtils.isNotEmpty(value)){
                value = String.valueOf(CommMath.addDouble(Double.parseDouble(value),CommMath.subDouble(roomPos.getPoint(), roomPos.getRoomSportsConsume())));
            }else {
                value = String.valueOf(CommMath.subDouble(roomPos.getPoint(), roomPos.getRoomSportsConsume()));
            }
            ContainerMgr.get().getRedis().putWithTime(key, 24 * 3600, value);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *
     * @return
     */
    public void addLeaveSharePlayer(long clubID, long pid, long roomID, double point) {
        try {
            String key = SHARE_LEAVE_PLAYER_KEY + ":" + clubID + ":" + pid + ":" + roomID;
            String value = ContainerMgr.get().getRedis().get(key);
            if(StringUtils.isNotEmpty(value)){
                value = String.valueOf(CommMath.addDouble(Double.parseDouble(value), point));
            }else {
                value = String.valueOf(point);
            }
            ContainerMgr.get().getRedis().putWithTime(key, 24 * 3600, value);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public double getClubLeaveSharePlayerPoint(long clubID) {
        double sumPoint = 0D;
        try {
            Set<String> keys = ContainerMgr.get().getRedis().keys(SHARE_LEAVE_PLAYER_KEY + ":" + clubID + "*");
            for(String x: keys){
                String value = ContainerMgr.get().getRedis().get(x);
                if(value != null){
                    sumPoint = CommMath.addDouble(sumPoint, Double.parseDouble(value));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return sumPoint;
    }

    /**
     *
     * @param pid
     * @return
     */
    public Double getLeaveSharePlayerPoint(long clubID, Long pid) {
        double sumPoint = 0D;
        try {
            Set<String> keys = ContainerMgr.get().getRedis().keys(SHARE_LEAVE_PLAYER_KEY + ":" + clubID + ":" + pid + "*");
            for(String x: keys){
                String value = ContainerMgr.get().getRedis().get(x);
                if(value != null){
                    try {
                        sumPoint = CommMath.addDouble(sumPoint, Double.parseDouble(value));
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return sumPoint;
    }

    /**
     *
     * @param roomPos
     */
    public void removeLeaveSharePlayer(AbsRoomPos roomPos) {
        try {
            String key = SHARE_LEAVE_PLAYER_KEY + ":" + roomPos.getClubMemberBO().getClubID() + ":" + roomPos.getPid() + ":" + roomPos.getRoom().getRoomID();
            ContainerMgr.get().getRedis().remove(key);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *
     */
    public void removeLeaveSharePlayer(long clubID, long pid, long roomID) {
        try {
            String key = SHARE_LEAVE_PLAYER_KEY + ":" + clubID + ":" + pid + ":" + roomID;
            ContainerMgr.get().getRedis().remove(key);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
